Basic Functions, Data Type and Modules


itertools

Functions creating iterators for efficient looping.

  • ### product, permutations and combinations

These functions can generate pairs of given iterables.

For example, given a list [1, 2, 3, 4]

For product function, will get the following pairs with the code " for pair in itertools.product(list, repeat=2) "

| 1,1 | 1,2 | 1,3 | 1,4 |

| 2,1 | 2,2 | 2,3 | 2,4 |

| 3,1 | 3,2 | 3,3 | 3,4 |

| 4,1 | 4,2 | 4,3 | 4,4 |

For permutations function, will get the following pairs with the code " for pair in itertools.permutations(list, r=2) "

| ... | 1,2 | 1,3 | 1,4 |

| 2,1 | ... | 2,3 | 2,4 |

| 3,1 | 3,2 | ... | 3,4 |

| 4,1 | 4,2 | 4,3 | ... |

For combinations function, will get the following pairs with the code " for pair in itertools.combinations(list, r=2) "

| ... | 1,2 | 1,3 | 1,4 |

| ... | ... | 2,3 | 2,4 |

| ... | ... | ... | 3,4 |

| ... | ... | ... | ... |